package cn.com.thingo.hyintelligent.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.model.bean.GatewayListBean;
import cn.com.thingo.hyintelligent.util.SPUtils;
import cn.com.thingo.hyintelligent.util.ToastUtil;

public class GatewayAdapter extends RecyclerView.Adapter<GatewayAdapter.ViewHolder> {
    private List<GatewayListBean.DataBean> list;
    private Context context;
    private OnModifyClickListener listener;
    private int currentPosition;

    public void setCurrentGatewayNum(int currentPosition) {
        this.currentPosition = currentPosition;
    }

    public GatewayAdapter(List<GatewayListBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_gateway, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        final GatewayListBean.DataBean dataBean = list.get(position);
        holder.tvName.setText(dataBean.getTitle());
        holder.tvNum.setText(dataBean.getNumber());
        if (currentPosition == position) {
            holder.ivSelector.setVisibility(View.VISIBLE);
        } else {
            holder.ivSelector.setVisibility(View.GONE);
        }
        if (listener != null) {
            holder.ivModify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onModifyClick(position);
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (position != currentPosition && !TextUtils.equals(list.get(position).getNumber(), list.get(currentPosition).getNumber())) {
                        listener.onSelectClick(position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.gateway_name)
        TextView tvName;
        @BindView(R.id.gateway_num)
        TextView tvNum;
        @BindView(R.id.gateway_modify)
        ImageView ivModify;
        @BindView(R.id.gateway_selector)
        ImageView ivSelector;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setListener(OnModifyClickListener listener) {
        this.listener = listener;
    }

    public interface OnModifyClickListener {
        void onModifyClick(int position);

        void onSelectClick(int position);
    }
}
